<template>
  <div class='risk-level-box flex'>
    <LevelBox :level="level"/>
    <div class="risk-content flex-1 flex-column flex flex-space-between flex-acenter">
      <div class="risk-level-tip">
        <h3 class="tip-title">风险等级</h3>
        <div class="tip-color" :class="`level-${level}`">蓝色</div>
      </div>
      <div class="risk-level-text flex flex-column flex-acenter">
        <h3 class="risk-number">98</h3>
        <p class="risk-title">安全评分</p>
      </div>
      <ul class="risk-legend flex">
        <li class="legend-item" v-for="lengend in lengendList" :key="lengend.color">
          <span class="legend-color" :style="{background: lengend.color}"></span>
          <span class="legend-text">{{ lengend.title }}</span>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import LevelBox from '@/components/Dashboard/LevelBox'
export default {
  components: {
    LevelBox
  },
  data () {
    return {
      // 风险等级
      level: 2,
      // 最大风险等级
      levelMax: 4,
      // 存储setTimeout的ID
      levelTimeoutId: null,
      // 循环换色
      isRunning: true,
      // 风险等级图例
      lengendList: [
        {
          title: '安全',
          color: '#66E1DF'
        },
        {
          title: '一级',
          color: '#FFD03B'
        },
        {
          title: '二级',
          color: '#FF6A3A'
        },
        {
          title: '三级',
          color: '#FF3838'
        }
      ]
    };
  },
  created () {
    this.startSetLevel()
  },
  mounted () {

  },
  computed: {
    levelImg () {
      const imgPath = require(`@/assets/images/dashboard/lev_${this.level}.png`)
      return imgPath
    },

  },
  watch: {
    level () {
      this.startSetLevel()
    }
  },
  methods: {
    handleError () {

    },
    setLevel () {
      this.levelTimeoutId = setTimeout(() => {
        const increase = this.level + 1
        this.level = increase
        if (this.level > this.levelMax) {
          this.level = 1
        }
        this.levelTimeoutId = null
      }, 5000);
      // this.setLevel()
    },
    startSetLevel () {
      this.isRunning = true
      this.setLevel();
    },
    stopSetLevel () {
      this.isRunning = false
    }
  },
  beforeDestroy () {
    this.stopSetLevel();
  },

}
</script>

<style lang='less' scoped>
.risk-level-box {
  width: 100%;
  height: 100%;
  align-items: center;
  .risk-img {
    // margin: 0 auto;
    width: 160px;
    height: 160px;
    background: url("@/assets/images/dashboard/risk_bg.png");
    .img {
      margin: 0 auto;
      transition: all 0.5s ease;
    }
  }
  .risk-content {
    display: flex;
    justify-content: center;
    .risk-level-tip {
      display: flex;
      flex-direction: column;
      align-items: center;
      .tip-title {
        font-family: @fontFamily;
        font-size: 20px;
        color: #d9e7ff;
      }
      .tip-color {
        font-family: @fontFamily;
        font-size: 16px;
        padding: 0 20px;
        position: relative;
        &::before,
        &::after {
          content: "";
          position: absolute;
          top: 10px;
          width: 30px;
          height: 4px;
          background-repeat: no-repeat;
          background-size: cover;
          transform: translateY(-50%);
        }
        &::before {
          background-image: url("@/assets/images/dashboard/dashed-left.png");
          left: -15px;
        }
        &::after {
          background-image: url("@/assets/images/dashboard/dashed-right.png");
          right: -15px;
        }
      }
      .level-1 {
        color: #66e1df;
      }
      .level-2 {
        color: #ffd03b;
      }
      .level-3 {
        color: #ff6a3a;
      }
      .level-4 {
        color: #ff3838;
      }
    }
    .risk-level-text {
      .risk-number {
        font-family: Oswald, Oswald;
        font-weight: 400;
        font-size: 42px;
        padding: 6px;
      }
      .risk-title {
        font-weight: 400;
        font-size: 14px;
        color: #ffffff;
        padding: 3px;
      }
    }
    .risk-legend {
      .legend-item {
        display: flex;
        justify-content: center;
        align-items: center;
        margin-right: 5px;
        cursor: pointer;
        .legend-color {
          display: inline-block;
          width: 8px;
          height: 8px;
          margin-right: 3px;
        }
      }
    }
  }
}
</style>